package content

import (
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"os"
	"woo-server/controllers"
	"woo-server/services/content"
)

// @tags 上传单图
// @summary 上传单图
// @Description 上传管理的上传单图
// @Accept json
// @Success 200 {string} json "{"StatusCode":200,"IsEncrypted":false,"Data":""}"
// @Router /api/File/OnlyUploadImage [get]
func OnlyUploadImage(c *gin.Context) {

	// common
	com := controllers.NewCommon(c, "OnlyUploadImage")
	UploadFileService := content.NewUploadFileService(com)
	result := UploadFileService.UploadFile(c)
	// 返回
	c.JSON(http.StatusOK, result)

}

// @tags 内容管理
// @summary 后台上传管理
// @Description 后台上传文件列表
// @Accept json
// @Success 200 {string} json "{"StatusCode":200,"Data":""}"
// @Router /api/Menu/List [get]
func UploadFileList(c *gin.Context) {
	// common
	com := controllers.NewCommon(c, "GetUploadFileList")

	// 生成登录态
	MenuService := content.NewUploadFileService(com)
	result := MenuService.GetFileList(c)

	// 返回
	c.JSON(http.StatusOK, result)
}

// 显示图片接口
func ShowImage(c *gin.Context) {
	file, err := os.Open("." + c.Query("imageName"))
	w := c.Writer
	errorHandle(err, w)

	defer file.Close()
	buff, err := ioutil.ReadAll(file)
	errorHandle(err, w)
	w.Write(buff)
}

// 统一错误输出接口
func errorHandle(err error, w http.ResponseWriter) {
	if err != nil {
		w.Write([]byte(err.Error()))
	}
}
